package leetcode.day_2021_03_22_2021_03_28;

import java.util.Arrays;

/**
 * 80. 删除排序数组中的重复项 II
 * https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/
 */
public class LeetCode0080 {
    public static int removeDuplicates(int[] nums) {
        int i=1, count=1, length = nums.length;
        while (i < length) {
            if (nums[i] == nums[i-1]) {
                count++;
                if (count > 2) {
                    removeNums(nums, i);
                    i--;
                    length--;
                }
            } else {
                count = 1;
            }
            i++;
        }
        return length;
    }

    public static void removeNums(int[] nums, int index) {
        for (int i=index+1; i<nums.length; i++) {
            nums[i-1] = nums[i];
        }
    }

    public static void main(String[] args) {
        int[] nums = {0,0,1,1,1,1,2,3,3};
        removeDuplicates(nums);
        System.out.println(Arrays.toString(nums));
    }
}
